package com.sharedlease.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharedlease.Feign.*;
import com.sharedlease.entity.*;
import com.sharedlease.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吴钟富
 * @since 2021-03-02
 */
@RestController
@Api(tags = "用户相关接口")
@RequestMapping("/user")
public class UserController {
    @Autowired
    public UserService userService;
    @Autowired
    public CommentClient commentClient;
    @Autowired
    public IndentClient indentClient;
    @Autowired
    public ProductClient productClient;
    @Autowired
    public Collect_merchantClient collect_merchantClient;
    @Autowired
    public Shop_carClient shop_carClient;

    @GetMapping("/getById/{id}")
    @ApiOperation("根据一个id查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的用户id", defaultValue = "1"),

    }
    )
    public User getById(@PathVariable Long id) {
        User user = userService.getById(id);
        return user;
    }

    @GetMapping("/list")
    @ApiOperation("查询全部用户")
    public List<User> list() {
        List<User> list = userService.list(null);
        return list;
    }

    @PostMapping("/save")
    @ApiOperation("新增一个用户")
    public R save(@RequestBody User user) {
        String encodePassword= Base64Utils.encodeToUrlSafeString(user.getPassword().getBytes());
        user.setPassword(encodePassword);
        return R.operate(userService.save(user));
    }

    @PutMapping("/update")
    @ApiOperation("根据id更改一个用户")
    public R update(@RequestBody User user) {
        if (userService.updateById(user)) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除一个用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的用户id", defaultValue = "-1"),

    }
    )
    public R removeById(@PathVariable Long id) {

        return R.operate(userService.removeById(id));
    }

    @GetMapping("/getCommentById/{id}")
    @ApiOperation("根据id得到一个评论")
    public Comment getCommentById(@PathVariable Long id) {
        return commentClient.getCommentById(id);

    }

    @GetMapping("/getCommentsByUid/{uid}")
    @ApiOperation("根据uid查询评论")
    public List<Comment> getCommentsByUid(@PathVariable Long uid) {
        return commentClient.getCommentsByUid(uid);

    }

    @PostMapping("/saveComment")
    @ApiOperation("新增一个评论")
    public R saveComment(@RequestBody Comment comment) {
        return commentClient.saveComment(comment);

    }

    @PostMapping("/deleteCommentById")
    @ApiOperation("根据id删除一个评论")
    public R deleteComment(@RequestBody Comment comment) {
        return commentClient.deleteCommentById(comment);

    }

    @PutMapping("/updateComment")
    @ApiOperation("根据id更改一个评论")
    public R updateComment(@RequestBody Comment comment) {
        return commentClient.updateCommentById(comment);
    }

    @GetMapping("/getIndentById/{id}")
    @ApiOperation("根据id得到一个订单")
    public Indent getIndentById(@PathVariable Long id) {
        return indentClient.getIndentById(id);

    }

    @GetMapping("/getIndentsByUid/{uid}")
    @ApiOperation("根据uid查询订单")
    public List<Indent> getIndentsByUid(@PathVariable Long uid) {
        return indentClient.getIndentsByUid(uid);

    }

    @PostMapping("/saveIndent")
    @ApiOperation("新增一个订单")
    public R saveIndent(@RequestBody Indent indent) {
        return indentClient.saveIndent(indent);

    }

    @PostMapping("/deleteIndentById")
    @ApiOperation("根据id删除一个订单")
    public R deleteIndent(@RequestBody Indent indent) {
        return indentClient.deleteIndentById(indent);

    }

    @GetMapping("/getProductById/{id}")
    @ApiOperation("根据id得到一个产品")
    public Product getProductById(@PathVariable Long id) {
        return productClient.getProductById(id);

    }

    @GetMapping("/getProductList")
    @ApiOperation("得到所有产品")
    public List<Product> getProductList() {
        return productClient.getProductList();

    }

    @PostMapping("/saveShop_car")
    @ApiOperation("新增一条购物车记录")
    public R saveShop_car(@RequestBody Shop_car shop_car) {
        return shop_carClient.saveShop_car(shop_car);

    }

    @PostMapping("/deleteShop_carById")
    @ApiOperation("根据id删除一条购物车记录")
    public R deleteShop_car(@RequestBody Shop_car shop_car) {
        return shop_carClient.deleteShop_carById(shop_car);

    }

    @PostMapping("/saveCollect_merchant")
    @ApiOperation("新增一个收藏厂商记录")
    public R saveMerchant(@RequestBody Collect_merchant collect_merchant) {
        return collect_merchantClient.saveCollect_merchant(collect_merchant);

    }

    @PostMapping("/deleteMerchantById")
    @ApiOperation("根据id删除一个收藏厂商记录")
    public R deleteMerchant(@RequestBody Collect_merchant collect_merchant) {
        return collect_merchantClient.deleteCollect_merchantById(collect_merchant);

    }

    @GetMapping("/getShop_carsByUid/{uid}")
    @ApiOperation("根据uid查询购物车")
    public List<Shop_car> getShop_carsByUid(@PathVariable Long uid) {
        return shop_carClient.getShop_carsByUid(uid);

    }

    @GetMapping("/getCollect_merchantByUid/{uid}")
    @ApiOperation("根据uid查询收藏厂商记录")
    public List<Collect_merchant> getCollect_merchantByUid(@PathVariable Long uid) {
        return collect_merchantClient.getCollect_merchantByUid(uid);

    }

    @PostMapping("/findByNameAndPwd/{name}/{password}")
    @ApiOperation("按用户名或手机号或者email跟密码查询普通用户")
    public User findByNameAndPwd(@PathVariable String name,@PathVariable String password) {
        String encodePassword= Base64Utils.encodeToUrlSafeString(password.getBytes());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.and(x ->
                x.eq("name", name).eq("password", encodePassword)
                        .or().eq("phone", name).eq("password", encodePassword)
                        .or().eq("email", name).eq("password", encodePassword)
        );
        return userService.getOne(wrapper);

    }

    @GetMapping("/findPwdByName/{name}")
    @ApiOperation("按用户名查询加密过后的密码")
    public String findByNameAndPwd(@PathVariable String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        User one = userService.getOne(wrapper);
        return  one.getPassword();

    }

    @GetMapping("/checkExistByName/{name}")
    @ApiOperation("判断用户的（用户名或者手机号或者邮箱）是否存在")
    public boolean checkExistByName(@PathVariable String name){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.and(x ->
                x.eq("name",name).or().eq("phone",name).or().eq("email",name)
        );

        User one;
        one= userService.getOne(wrapper);
        if (one!=null){
            return true;

        }
        else{
            return false;
        }

    }

}

